package com.sirui.x.learnIo.netty.handle;


import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

public class ReadLineHandle extends ChannelInboundHandlerAdapter{

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		ByteBuf buffer = (ByteBuf)msg;
		if(buffer.readableBytes() > 0) {
			System.out.println("Server received: " + buffer.toString(CharsetUtil.UTF_8));
			ctx.write(buffer);
		}else {
			System.out.println("readableBytes: 0");
		}
		
	}
	
	
	@Override
	public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
		
		System.out.println("channel read complete.");
//		ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
	}
	
	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		
		cause.printStackTrace();
		super.exceptionCaught(ctx, cause);
	}
}
